package com.bipa.controller;

import com.bipa.bean.User;
import com.bipa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RegisterController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String register(){
        return "front/register";
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ModelAndView register(User user, HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        user.setCreate_date(new java.sql.Date(new  java.util.Date().getTime()));
        int rows = userService.register(user,request.getContextPath());
        if (rows == 1) {
            mv.addObject("message", "注册成功,请登录邮箱【"+user.getEmail()+"】进行激活！");
        }else {
            mv.addObject("message", "注册失败！");
        }
        mv.setViewName("front/register");
        return mv;
    }

    @RequestMapping("/userActive")
    public ModelAndView activeUser(String active){
        ModelAndView mv = new ModelAndView();
        userService.activeUser(active);
        mv.addObject("msg", "恭喜您，激活成功！");
        mv.setViewName("front/login");
        return mv;
    }
}
